﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace ERP.Manager.Web.Util
{

    public static class PagerHelper
    {
        private const string REQUEST_PAGEINDEX_NAME = "page";
        private const int PAGEER_COUNT = 13;
        private static string formatPager = string.Empty;

        public static string AjaxPager(this HtmlHelper html, int pageSize, int totalCount)
        {
            formatPager = "javascript:to_page({0});";

            return GeneratePager(html, pageSize, totalCount);
        }

        public static string NormalPager(this HtmlHelper html, int pageSize, int totalCount)
        {
            formatPager = "?page={0}";

            return GeneratePager(html, pageSize, totalCount);
        }

        public static string NormalPager(this HtmlHelper html, int pageSize, int totalCount, string otherParams)
        {
            formatPager = "?page={0}&" + otherParams + "'";

            return GeneratePager(html, pageSize, totalCount);
        }

        private static string GeneratePager(HtmlHelper html, int pageSize, int totalCount)
        {
            var totalPagesCount = Math.Max((totalCount + pageSize - 1) / pageSize, 1);
            if (totalPagesCount <= 1) return string.Empty;

            var currentPageIndex = GetCurrentPage(html, totalPagesCount);

            var output = new StringBuilder();
            output.Append("<table border='0px'><tr><td>总共有:&nbsp;<font color='red'>" + totalCount + "</font>&nbsp;条数据&nbsp;&nbsp;</td><td>当前第" + currentPageIndex + "/" + totalPagesCount + "页</td>");
            HandleFirstPage(output, currentPageIndex);
            HandlePreviousPage(output, currentPageIndex);
            HandlePagers(output, totalPagesCount, currentPageIndex);
            HandleNextPage(output, totalPagesCount, currentPageIndex);
            HandleLastPage(output, totalPagesCount, currentPageIndex);

            output.Append("</tr></table>");

            return output.ToString();
        }

        private static void HandleFirstPage(StringBuilder output, int currentPageIndex)
        {
            if (currentPageIndex == 1) output.Append(GetLiTag(GetATag("首页", "javascript:void(0)")));
            else
            {
                output.Append(GetLiTag(GetATag("首页", GetAHref(1))));
            }
        }

        private static void HandlePreviousPage(StringBuilder output, int currentPageIndex)
        {
            if (currentPageIndex == 1)
            {
                output.Append(GetLiTag(GetATag("上一页", "javascript:void(0)")));
            }
            else
            {
                output.Append(GetLiTag(GetATag("上一页", GetAHref(currentPageIndex - 1))));
            }
        }

        private static void HandlePagers(StringBuilder output, int totalPagesCount, int currentPageIndex)
        {
            int startIndex = GetStartIndex(totalPagesCount, currentPageIndex);

            for (var i = 0; i < PAGEER_COUNT; i++)
            {
                int pageIndex = startIndex + i;

                if (pageIndex > totalPagesCount) break;

                if (currentPageIndex == pageIndex) output.Append(GetLiTag(pageIndex, "current"));
                else output.Append(GetLiTag(GetATag(pageIndex, GetAHref(pageIndex))));
            }
        }

        private static void HandleNextPage(StringBuilder output, int totalPagesCount, int currentPageIndex)
        {
            if (currentPageIndex < totalPagesCount) output.Append(GetLiTag(GetATag("下一页", GetAHref(currentPageIndex + 1))));
            else
            {
                output.Append(GetLiTag(GetATag("下一页", "javascript:void(0)")));
            }
        }

        private static void HandleLastPage(StringBuilder output, int totalPagesCount, int currentPageIndex)
        {
            if (currentPageIndex < totalPagesCount) output.Append(GetLiTag(GetATag("尾页", GetAHref(totalPagesCount))));
            else
            {
                output.Append(GetLiTag(GetATag("尾页", "javascript:void(0)")));
            }
        }

        private static int GetCurrentPage(HtmlHelper html, int totalPagesCount)
        {
            var queryString = html.ViewContext.HttpContext.Request.QueryString;
            var currentPageIndex = 1;
            int.TryParse(queryString[REQUEST_PAGEINDEX_NAME], out currentPageIndex);

            if (currentPageIndex <= 0) currentPageIndex = 1;
            if (currentPageIndex > totalPagesCount) currentPageIndex = totalPagesCount;
            return currentPageIndex;
        }

        private static int GetStartIndex(int totalPagesCount, int currentPage)
        {
            var leftEdge = currentPage - PAGEER_COUNT / 2;
            var startIndex = leftEdge <= 0 ? 1 : leftEdge;
            var rightEdge = startIndex - 1 + PAGEER_COUNT;
            if (totalPagesCount >= PAGEER_COUNT && rightEdge > totalPagesCount) startIndex -= rightEdge - totalPagesCount;

            return startIndex;
        }
        private static string GetLiTag(int pageIndex, string className)
        {
            return GetLiTag(pageIndex.ToString(), className);
        }

        private static string GetLiTag(string inner)
        {
            return GetLiTag(inner, string.Empty);
        }

        private static string GetLiTag(string inner, string className)
        {
            return string.Format("<td{0}>{1}</td>", className == string.Empty ? "" : " class='" + className + "'", inner);
        }

        private static string GetATag(int pageIndex, string href)
        {
            return GetATag(pageIndex.ToString(), href);
        }

        private static string GetATag(string inner, string href)
        {
            return string.Format("<a href='{0}'>{1}</a>", href, inner);
        }

        private static string GetAHref(int pageIndex)
        {
            return string.Format(formatPager, pageIndex);
        }
    }
}
